{extend name="base/form" /}
{block name="title"} 首页 {/block}
<!-- Page Content -->
{block name="content"}
<form class="layui-form">

    <div class="layui-form-item">
        <label class="layui-form-label">跳转链接</label>
        <div class="layui-input-block">
            <input type="text" id="url"  name="url"  autocomplete="off" placeholder="请输入链接地址" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">应&nbsp;&nbsp;&nbsp;用</label>
        <div class="layui-input-block">
            <input type="radio" name="type" checked value="1" title="PC">
            <input type="radio" name="type" value="2" title="手机" >
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">位&nbsp;&nbsp;&nbsp;置</label>
        <div class="layui-input-block">
            <input type="radio" name="site"  value="1" title="左上角">
            <input type="radio" name="site"  value="2" title="右上角" >
            <input type="radio" name="site" checked  value="3" title="轮播" >
            <input type="radio" name="site"   value="4" title="底部" >
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">图&nbsp;&nbsp;&nbsp;片</label>
        <div class="layui-input-block">
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-sm" id="test1">上传图片</button>
                <p class=""> 图片位尺寸说明：</p>
                <p>PC  1、左右上角（270px * 80px） 2、播图（580px * 230px） 3、底部（350px * 260px）</p>
                <p>手机 1、顶部（即:左上角)（200px * 80px） 2、轮播（750px * 314px)</p>
                <p style="color: red">注* 1、手机没有右上角图片位和底部图片位，只有左上角一张，轮播多张</p>
                <p style="color: red">注* 2、都只支持上传 png 、gif 后缀格式的图片</p>
                <p style="color: red">注* 3、图片大小限制1M以内</p>
                <input type="hidden" name="img" id="img">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1" width="100px" height="100px">
                    <p id="demoText"></p>
                </div>
                <div style="width: 95px;">
                    <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                        <div class="layui-progress-bar" lay-percent=""></div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">排&nbsp;&nbsp;&nbsp;序</label>
        <div class="layui-input-block">
            <input type="text" id="sort"  name="sort"  autocomplete="off" value="100" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button style="margin-left: 20%" class="layui-btn" lay-submit="*" lay-filter="addSubmit">提交</button>
            <button id="reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>


</form>
<!-- /Page Content -->
{/block}
{block name="js"}
<script>
    layui.use(['form','layer','upload'], function(){
        var $ = layui.jquery
            ,upload = layui.upload
            ,element = layui.element
            ,layer = layui.layer
            ,form =  layui.form

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: "{:url('upload')}" //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            ,done: function(res){
                //如果上传失败
                if(res.code == 0){
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                    return layer.msg(res.msg,{icon:5});
                }
                //上传成功的一些操作
                console.log(res)
                $("#img").val(res.src)
                //……
                $('#demoText').html(''); //置空上传失败的状态
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            //进度条
            ,progress: function(n, elem, e){
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        //当你在iframe页面关闭自身时
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        //监听提交
        form.on('submit(addSubmit)', function(data){
            console.log(data.field)
            $.ajax({
                url:"{:url('addPost')}",
                type:'post',
                data:data.field,
                success:function(e){
                    if(e.code == 1){
                        layer.msg(e.msg,{icon:6,time:2000},function (){
                            parent.layer.close(index); //再执行关闭
                            window.location.reload()
                        })
                    }else{
                        layer.msg(e.msg,{icon:5,time:2000},function (){
                            parent.layer.close(index); //再执行关闭
                            window.location.reload()
                        })
                    }
                }
            });
            return false;
        });
    });
</script>
{/block}